# @time     :2022/2/2
# @Author   :Simple
# @FileName :request_api
import requests

from util.logger import log


def send_request(api: dict, session: requests.Session() = None):
    """
    发送请求
    :param session: 所有请求使用同一个session（带有公共参数（参数会自动带入请求））
    :param api: 接口api
    :return: json格式的response
    """
    if session is None:
        return requests.request(**api).json()
    else:
        return session.request(**api).json()


# def pre_api(response: dict, pre_param: str):
#     """
#     从response中按pre_param规则提取数据
#     :param response:
#     :param pre_param: 提取规则
#     例如 从{"name1":{"name2":{"name3":"value3"}}}中提取"value3"，则pre_param为"name1.name2.name3"
#     :return:
#     """
#     new_response = response
#     keys = pre_param.split(".")
#     if len(keys) < 1:
#         log.warnning(f" {pre_param} 为空，提取数据失败")
#         return
#     try:
#         for i in range(len(keys)):
#             response = response[keys[i]]
#         log.debug(f" {new_response} 提取数据 {pre_param} 成功！")
#         return response
#     except:
#         log.warnning(f" {new_response} 提取数据 {pre_param} 失败！")
#         return


# if __name__ == "__main__":
#
#     print(pre_api({"name1":{"name2":{"name3":"value3"}}}, "name1.name2"))
